﻿package common 
{
	import adobe.utils.CustomActions;
	import flash.utils.ByteArray;
	import game.MainGame;
	import game.GameBase;
	import game.constants.ConstData;
	import flash.utils.setTimeout;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class Utility {
		private static var mainGame:MainGame;
		private static var game:GameBase;
		
		public function Utility() {	}
		
		/**
		 * @author Daisy
		 * @param	result
		 * @usage To check the result returned from FMS success or not
		 * @return 
		 */
		public static function checkResult(result:String):Boolean {
			if (result.substr(ConstData.POS_SUCCESS_RESULT, ConstData.LEN_SUCCESS_RESULT) == "0") {
				return true;
				
			}
			return false;
		}		
		
		public static function checkTokenInvalid(_res:String):Boolean {
			var temp:String = _res.substr(ConstData.LEN_SUCCESS_RESULT, _res.length);	
			if (temp == "Token Invalid") {				
				return true;
			}
			return false;
		}
		
		public static function clone(source:Object):* {
			var byteArr:ByteArray = new ByteArray();
			byteArr.writeObject(source);
			byteArr.position = 0;
			return(byteArr.readObject());
		}
		
		public static function fixNumber(n:Number):Number {
		
				return Math.round(n * 100000000) / 100000000;
		}	
		
		public static function setGame(g:GameBase):void {
			game = g;
		}
		/**
		 * @author	Bamboo
		 * @param	month
		 * @return
		 */
		public static function getMonthNumber(month:String):int {
			var monthsArr:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
			for(var i:int; i< monthsArr.length; i++){
				if(month == monthsArr[i])
					return i+1;
			}
			return 0;
		}
		/**
		 * @author	Bamboo
		 * @param	month
		 * @return
		 */
		public static function getMonthString(month:Number):String {
			var monthArr:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
			return monthArr[month -1];
		}
		
		/**
		 * @author	Bamboo
		 * @param	pStr
		 * @return
		 */
		public static function trim(pStr:String):String {
			var ltrimPos:int = -1;
			var space:Boolean = true;
			var tmpStr = pStr;
			for (var i:int =0; ((i<tmpStr.length) && space); i++) {
				if (tmpStr.substr(i, 1) == " ") {
					ltrimPos = i;
				} else {
					space = false;
				}
			}
			if (ltrimPos>=0) {
				tmpStr = tmpStr.substr(ltrimPos+1, tmpStr.length);
			}
			space = true;
			var rtrimPos:int = tmpStr.length;
			for (i=tmpStr.length-1; ((i>0) && space); i--) {
				if (tmpStr.substr(i, 1) == " ") {
					rtrimPos = i;
				} else {
					space = false;
				}
			}
			if (rtrimPos<tmpStr.length) {
				tmpStr = tmpStr.substr(0, rtrimPos);
			}
			return tmpStr;
		}
		/**
		 * @author	Bamboo
		 * @param	num
		 * @param	decimalPlace
		 * @param	currency
		 * @return  fomat number
		 */
		public static function currency(num:Number,decimalPlace:Number=2,currency:String="$"):String
        {
            //assigns true boolean value to neg in number less than 0
            var neg:Boolean = (num < 0);
           
           //make the number positive for easy conversion
            num = Math.abs(num);
			//not round
			var str:String = num.toString();
		    var tem:Array = str.split(".");
			if (tem.length > 1)
			{		
				str = tem[1];
				str = str.substr(0, 2);
				num = Number(tem[0] + "." + str);
			}
			//end
            var roundedAmount:String = String(num.toFixed(decimalPlace));//do round
			
	       //split string into array for dollars and cents
            var amountArray:Array = roundedAmount.split(".");
            var dollars:String = amountArray[0];
            var cents:String = amountArray[1];
		    //create dollar amount
            var dollarFinal:String = "";
            var i:int = 0;
            for (i; i < dollars.length; i++)
            {
                if (i > 0 && (i % 3 == 0 ))
                {
                    dollarFinal = "," + dollarFinal;
                }
               
                dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
            }  
           
            //create Cents amount and zeros if necessary
            var centsFinal:String = String(cents);
           
            var missingZeros:int = decimalPlace - centsFinal.length;
       
            if (centsFinal.length < decimalPlace)
            {
                for (var j:int = 0; j < missingZeros; j++)
                {
                    centsFinal += "0";
                }
            }
           
            var finalString:String = "";

            if (neg)
            {
                finalString = "-" + currency + dollarFinal;
            } else
            {
                finalString = currency + dollarFinal;
            }

            if(decimalPlace > 0)
            {
                finalString += "." + centsFinal;
            }
            /*trace("finalString:"  + finalString);*/
            return finalString;
        }
		
		/**
		 * @author	bamboo
		 * @param	a
		 * @param	n
		 */
		public static function bubbleSort(a:Array, n:int):void
		{
			var i, j, temp:int;
			for (i = 0; i < n; i++)
			{
				for (j = 0; j < n; j++)
		
					if (a[j] > a[j + 1])
					{
						temp = a[j];
						a[j] = a[j + 1];
						a[j + 1] = temp;
					}
			}
		}
		/**
		 * @author	bamboo
		 * @param	a
		 * @param	n
		 */
		public static function selectionSort(a:Array,n:int)//selection sort function
		{
			var i, j, min, pmin:int;
			
			for (i = 0; i < (n - 1); i++)
			{
				pmin = i;
				
				min = a[i];
		
				for (j = i + 1; j < (n); j++) //select the min of the rest of array
				{
					if (min > a[j])   //ascending order for descending reverse
					{
						pmin = j;  //the position of the min element
					  
						min = a[j];
					  
					}
				}
				var temp:int = a[i] ;
				
				a[i] = a[pmin];  //swap
				
				a[pmin] = temp; 
				
			}
		}
		
		public static function compare(a:int, b:int, c:int):int {
			trace("a" + a +"b"+ b +"c"+ c);
			var min:int;
			if (a <= b) {
				min = a;
			}else {
				min = b;
			}	
			if (min <= c)
			 return min;
			return c;
		}
	}
}